<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>

</body>
</html>
<script>

  class List {
    constructor(val, left, right) {
      this.val = val;
      this.left = left;
      this.right = right
    }
  }


  let obj = new List(1, null, null);
  let box = new List(2, null, null);
  let box1 = new List(3, null, null);
  let box2 = new List(4, null, null);
  let box3 = new List(5, null, null);
  obj.left = box;
  obj.right = box1;
  box.left = box2;
  box.right = box3;

  const Qian = (obj) => {
    if (!obj) {
      return obj
    }
    console.log(obj.val);
    Qian(obj.left);
    Qian(obj.right)
  };
  Qian(obj);
  /*马凯薇*/
  const middle = (obj) => {
    if (!obj) return;
    const stack = [obj]; // 先进后出 后进的先出 left
    let p = root;
    while (stack.length || p) {
      while (p) {
        stack.push(p);
        p = p.left;
      }
      const n = stack.pop();
      console.log(n.val);
      p = n.right;
      //
      // const n = stack.pop();
      // if (n.right) {
      //   stack.push(n.right)
      // }
      // if (n.left) {
      //   stack.push(n.left);
      // }
    }
  };
  middle();
  // zhong(obj)
  // fun(obj)
  function fun(obj) {
    if (!obj) {
      return obj
    }
    let task = [obj];
    while (task.length) {
      let n = task.pop();
      console.log(n.val);
      if (n.left) {
        task.push(n.left)
      }
      if (n.right) {
        task.push(n.rights)
      }
    }
  }

  //赵孟凡
  function ceNode(root) {
    if (!root) return null;
    let arr = [root];
    let res = [];
    while (arr.length) {
      let n = arr.shift();
      res.push(n.val);
      n.left && arr.push(n.left);
      n.right && arr.push(n.right);
    }
    return res
  }

  console.log(ceNode(obj));

</script>
